<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix='fmt' uri="http://java.sun.com/jsp/jstl/fmt"%>
<div class="page-header">
	<h3>自定义APP</h3>
</div>
<ul class="nav nav-tabs">
		<li class="active"><a href="#">菜单列表</a></li>
	</ul><br/>
<div  class="col-md-8 column">
	<div id="tree"  style="height:600px; overflow: auto;"></div>
	<!-- <button type="button" class="btn btn-default" id="delete">删除</button> -->
</div> 
<div class="panel panel-default" style="display:none; width: 350px;" id="addPanel">
		<div class="panel-heading">
				<h3 class="panel-title">添加节点</h3>
		</div><br>
		<div class="panel-body">
			<input name="id" type="hidden" value=""/>
			<input name="parentId" type="hidden" value=""/>
			<label for="name">功能名称</label><input name="name" type="text"class="form-control" id="name" /><br /> 
			<label for="description">功能描述</label><input name="description" type="text"class="form-control" id="description" /><br /> 
			<label for="uuid">UUID</label><input name="uuid" type="text"class="form-control" id="uuid" readonly="readonly"/><br /> 
			<button type="button" class="btn btn-default pull-right" id="save">保存</button>
		</div>
		
</div>
<script type="text/javascript">
	$(function(){
		$('#tree').treeview({data: getAppTree(),showTags:true});
		$("#save").click(function() {
			update();
		});
		$("#export").click(function() {
			location.href="${basePath}/appFunction/export";  
		});
		$("#add").click(function() {
			add();
		});
		$("#delete").click(function() {
			deleteById();
		});
		if(!Array.indexOf)
		{
		    Array.prototype.indexOf = function(obj)
		    {             
		        for(var i=0; i<this.length; i++)
		        {
		            if(this[i]==obj)
		            {
		                return i;
		            }
		        }
		        return -1;
		    }
		}
	});
	
	function getAppTree(){
		var result ;
		$.ajax({ 
			url:"${basePath}/appFunction/treeData",
			async:false,
			type:'post',
			data:{id:$("select[id=selectProduct] :selected").attr("pid")},
			success:function(data) {
				if (data.result.success) {
					result = data.result.object;
					if(result.length==0){
						$("[name=parentId]").val(1);//当树节点为空时，默认顶级节点为父ID
					}
				} else {
					layer.msg(data.result.message, {
						icon : 2,
						time : 2000
					});
				}
			}
		})
		return result;
	}
	
	function deleteById(n){
		var nodeId = $(n).parent().attr('data-id');
		if(!nodeId){
			layer.msg("请选择要删除的菜单", {
				icon : 2,
				time : 2000
			});
		}else{
			if(confirm("删除节点会删除该节点下所有节点，是否删除？")){
				//删除
				$.post("${basePath}/appFunction/delete", {
					id : nodeId,productId:$("select[id=selectProduct] :selected").attr("pid")
				}, function(data) {
					if (data.result.success) {
						//currNodeId = null;
						layer.msg(data.result.message, {
							icon : 1,
							time : 1000
						}, function() {
							$("#rightDiv").load("${basePath}/appFunction/list",{productId:$("select[id=selectProduct] :selected").attr("pid")});
						});
					} else {
						layer.msg(data.result.message, {
							icon : 2,
							time : 2000
						});
					}
				});
			}
		}
	}
	var currNodeId ;
	var nodeText ;
	function itemOnclick(node){
		currNodeId = $(node).parent().attr('data-id');
		nodeText = $(node).text();
		$.post("${basePath}/appFunction/updateView",{id:currNodeId},function(data) {
			if (data.result.success) {
				$("[name=id]").val(data.result.object.appFunction.id);
				$("[name=parentId]").val(data.result.object.appFunction.parentId);
				$("[name=name]").val(data.result.object.appFunction.name);
				$("[name=description]").val(data.result.object.appFunction.description);
				$("[name=uuid]").val(data.result.object.appFunction.uuid);
				$(".panel-title").text("修改节点");
				window.editLayer = layer.open({
					type : 1,
					title : false,
					closeBtn : 1,
					shadeClose : true,
					content : $('#addPanel')
				});
			} else {
				layer.msg(data.result.message, {
					icon : 2,
					time : 2000
				});
			}
		});
	}
	function update(){
		var id = $("[name=id]").val();
		var productId = $("select[id=selectProduct] :selected").attr("pid");
		var parentId = $("[name=parentId]").val();
		var name = $("[name=name]").val();
		var description = $("[name=description]").val();
		//var uuid = $("[name=uuid]").val();
		if(!parentId){
			layer.msg("请选择上级菜单");
		}else if(!name){
			layer.msg("请填写菜单名");
		}else{
			$.post("${basePath}/appFunction/update",
					{id:id,productId:productId,parentId:parentId,name:name,description:description},
					function(data) {
				if (data.result.success) {
					layer.close(editLayer);
					layer.msg(data.result.message, {
						icon : 1,
						time : 1000
					},function(){
						$("#rightDiv").load("${basePath}/appFunction/list",{productId:$("select[id=selectProduct] :selected").attr("pid")});
					});
				} else {
					layer.msg(data.result.message, {
						icon : 2,
						time : 2000
					});
				}
			});
		}
	}
	
	/**
		清空表单数据
	*/
	function add(n){
		//event.stopPropagation(); //阻止事件冒泡
		/*$(".node-tree").each(function(){//节点背景颜色
			if($(n).parent().attr('data-nodeid') == $(this).attr('data-nodeid')){
				$(this).css('background-color','#428bca');
			}else{
				$(this).css('background-color','');
			}
		})*/
		var nodeId = $(n).parent().attr('data-id');
		$("[name=id]").val("");
		$("[name=parentId]").val(nodeId);//改变父节点ID
		$("[name=name]").val("");
		$("[name=description]").val("");
		$("[name=uuid]").val("");
		$(".panel-title").text("添加节点");
		window.editLayer = layer.open({
			type : 1,
			title : false,
			closeBtn : 1,
			shadeClose : true,
			content : $('#addPanel')
		});
	}
</script>